package com.base.myproject.client.HR.Training;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Training_Task_list extends BaseJDBCPanel{
	TabPanel tabPanel = null; 
	TreePanel<TreeNodeModelData> treePanel = null;
	// 创建树形结构的数据表
	BaseTreeStore<TreeNodeModelData> treeStore = null;
	StoreFilterField<BusiObject> ajaxfield;//类似ajax的自动查询框
	String results = null;
	String bcode = Env.getEnvbcode();
	
	public Training_Task_list(){
		String sql = "select substring('id like '''+replace(permissionsbcode,';','%'' or id like '''),9," +
		"len('id like '''+replace(permissionsbcode,';','%'' or id like '''))-21) as result from" +
		" busipermissions where bcode = '"+bcode+"' and busicode = 8010";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sql, new AsyncCallback<DataSet>(){
		@Override
			public void onFailure(Throwable caught) {	
			}
			@Override
			public void onSuccess(DataSet result) {  
			   results = result.getValue(0, "result");
			   init();
			}
		});
	}
	public void init(){
		initwidget();
		registerMainStore(store);
		registerStore(store);
	}
	public void complete()
	{
		super.complete();
		//getToolbarll().add(to_out);
		
	}
	private void initwidget() {
		HorizontalPanel hp=new HorizontalPanel();
		hp.add(getTreePanel());
		
		VerticalPanel   vp=new VerticalPanel();
		vp.setSpacing(1);
		FieldSet fs = new FieldSet();
		fs.setWidth(760);
		fs.setHeight(38);
	//	fs.setStyleName("firldset_css1");
		fs.setHeading("查询操作");
		fs.add(cread_field());
		vp.add(fs);
		vp.add(getgrid());
		vp.add(getBaseFormPanel());
		
		hp.add(vp);
		
		add(hp);
		this.layout();
	}
	TreePanel<TreeNodeModelData> getTreePanel() {
		if (treePanel == null) {
			initTreePanel();
		}
		return treePanel;
	}
	BaseTreeStore<TreeNodeModelData> getTreeStore() {
		if (treeStore == null) {
			initTreeStore();
		}
		return treeStore;
	}
	BaseTreeStore<TreeNodeModelData> initTreeStore() {
		StoreInfo si = new StoreInfo();
		String sql2 = "select icode,id,name from SystemInfo where id like "+results+" order by id asc";
		si.setSql(sql2);
		si.setTreesortcolumn("id");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("SystemInfo");
		treeStore = new BaseTreeStore<TreeNodeModelData>(si, false);
		treeStore.load();
		return treeStore;
		
	}
	// 实例化TreePanel
	TreePanelSelectionModel<TreeNodeModelData> tpsm = new TreePanelSelectionModel<TreeNodeModelData>();
	void initTreePanel() {
		//super.onRender();  
		treePanel = new TreePanel<TreeNodeModelData>(getTreeStore());
		treePanel.setWidth(150);
		treePanel.setBorders(true);
		treePanel.setHeight(450);
		treePanel.setDisplayProperty("name");
		treePanel.setSelectionModel(tpsm);
		treePanel.setCheckStyle(CheckCascade.PARENTS); 
		treePanel.setCheckNodes(CheckNodes.BOTH);  
		treePanel.setCheckable(false);
		treePanel.setAutoLoad(true);
		treePanel.addListener(Events.OnClick, new Listener<TreePanelEvent<TreeNodeModelData>>() {   
			public void handleEvent(TreePanelEvent<TreeNodeModelData> be) {   
				final String id=be.getItem().get("id").toString();//部门id
				if(!id.equals("00")){//表示为主节点 不因有任何操作
					 String sql="select  count(*) as c from systeminfo where len('"+id+"')=(select len(max(id)) from  systeminfo)";
					GWT.log(sql);
					 final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
					   greetingService2.getDataSet(sql, new AsyncCallback<DataSet>(){
						@Override
						public void onFailure(Throwable caught) {	
						}
						@Override
						 public void onSuccess(DataSet result) {  
			   			   if(!result.getValue(0, "c").equals("0")){
			   				GWT.log("aa");
			   				  /** String where_in="";
			   				   for(int i=0;i<result.getRowCount();i++){
			   					where_in+=result.getValue(i, "icode")+",";//"'"+result.getValue(0, "h"+(i+1))+"',";
			   				   }
			   				where_in=where_in.substring(0, where_in.length()-1);//获得当前部门的符合数据 icode  **/
			   				set_grid(id);
			   			   }
			   			GWT.log("bbb");
			   					   
						  }
					     });	   
				 }
				
	  		} 
		    });   
	}
	void set_grid(String id){
		GWT.log("id:"+id);
		String sql=" select * from hr_InitialJobTaskList where sectionCode ='"+id+"'";
		grid.getBg().getStore().getStoreinfo().setSql(sql);
		grid.getBg().getStore().reload();
		grid.getBg().getView().refresh(true);
	
	}
	
	BaseStore<BusiObject> store = null;
	BaseStore<BusiObject> getStore(){
	if(store==null) 
	{
		createStore();
	}
	return store;}
	ModelType type;
	BaseStore<BusiObject> createStore(){
	 type = new ModelType();
	type.setRoot("records");
	type.setRecordName("record");
	type.setTotalName("totle");
	type.addField("icode");
	type.getField("icode").setType(Long.class);

	type.addField("sectionCode");
	type.getField("sectionCode").setType(String.class);
	type.addField("taskNo");
	type.getField("taskNo").setType(String.class);
	type.addField("taskName");
	type.getField("taskName").setType(String.class);
	type.addField("content");
	type.getField("content").setType(String.class);

	StoreInfo info = new StoreInfo();
	info.setTablename("hr_InitialJobTaskList");
	info.setKeyname(new String[] { "icode"});
	info.setSql("select * from hr_InitialJobTaskList");
	  store = new BaseStore<BusiObject>(type, info);
	 return store;}
	
	BaseGridPanel grid=null;
	BaseGridPanel getgrid(){
		if(grid==null){
			createGrid();
		}
		return grid;
	}
	void createGrid(){
	CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
	List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
	columns.add(cbs.getColumn());

	//ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
	//columns.add(C1);
	
	ColumnConfig C3 = new ColumnConfig("sectionCode", "分部门", 80);
	BaseComboBoxForm<BaseModelData> bsb2 = new BaseComboBoxForm<BaseModelData>("id","name","systeminfo","where len(id)=(select len(max(id)) from  systeminfo)",true);
	C3.setRenderer(bsb2);
	C3.setEditor(bsb2.getCelEditor());
	columns.add(C3);
	
	ColumnConfig C4 = new ColumnConfig("taskNo", "任务号", 80);
	TextField<String> t2 = new TextField<String>();
	C4.setEditor(new CellEditor(t2));
	columns.add(C4);
	
	ColumnConfig C5 = new ColumnConfig("taskName", "任务名称", 80);
	TextField<String> t5 = new TextField<String>();
	C5.setEditor(new CellEditor(t5));
	columns.add(C5);
	
	/**ColumnConfig C6 = new ColumnConfig("content", "任务分解", 80);
	TextArea mark= new TextArea();
	//mark.setFireChangeEventOnSetValue(true);
	mark.setWidth(100);
	mark.setHeight(30);
	//C6.setEditor(new CellEditor(mark));
	C6.setEditor);
	columns.add(C6);**/
	
	
	ColumnModel cm = new ColumnModel(columns);
	grid = new BaseGridPanel(getStore(), cm,true,true);
	cbs.setSelectionMode(SelectionMode.SINGLE);
	grid.getBg().setSelectionModel(cbs);
	grid.getBg().setWidth(760);
	grid.getBg().setHeight(219);
	}
	
	BaseFormPanel form = null;
	 BaseFormPanel getBaseFormPanel() {
			if (form == null) {
				createForm();
			}
			return form;
		 }
	 TextArea mark;
	 void createForm() {
			form = new BaseFormPanel(getStore(),false);
			TableLayout tl = new TableLayout(6);
			TableData td = new TableData();
			td.setRowspan(1);
			td.setHorizontalAlign(HorizontalAlignment.LEFT);
			td.setVerticalAlign(VerticalAlignment.TOP);
			td.setPadding(2);
			TableData td2 = new TableData();
			td2.setRowspan(1);
			td2.setColspan(5);
			td2.setHorizontalAlign(HorizontalAlignment.LEFT);
			td2.setVerticalAlign(VerticalAlignment.TOP);
			td2.setPadding(2);
			
			form.setLayout(tl);
			form.setHeaderVisible(false);



	form.add(new Label("分部门:"),td);
	BaseComboBoxForm<BaseModelData> bsb2 = new BaseComboBoxForm<BaseModelData>("id","name","systeminfo","where len(id)=(select len(max(id)) from  systeminfo)",true);
	bsb2.setName("sectionCode");
	bsb2.setWidth(127);
	form.add(bsb2,td);
	form.add(new Label("任务号:"),td);
	TextField<String> C_num3  = new TextField<String>(); 
	C_num3.setName("taskNo");
	form.add(C_num3,td);
	form.add(new Label("任务名称:"),td);
	TextField<String> C_num4  = new TextField<String>(); 
	C_num4.setName("taskName");
	form.add(C_num4,td);
	form.add(new Label("任务分解:"),td);
	mark = new TextArea();
	mark.setFireChangeEventOnSetValue(true);
	mark.setWidth(488);
	mark.setHeight(99);
	mark.setName("content");
	form.add(mark,td2);
	
	 form.inited(); 
	};
	
	/**
	 * 过滤
	 * @param store
	 * @param parent
	 * @param record
	 * @param property
	 * @param filter
	 * @return  ajax 查询
	 */
	protected boolean Select(Store<BusiObject> store,
			BusiObject parent, BusiObject record, String property,
			String filter) {
		for (int i = 0; i < type.getFieldCount(); i++) {
			Object name = record.get(type.getField(i).getName());	
			//TODO
			if (name!=null&&name.toString().indexOf(filter.toLowerCase()) != -1) {
				return true;
			}
		}
		return false;
	}	
	StoreFilterField cread_field(){
		ajaxfield = new StoreFilterField<BusiObject>() {
			@Override
			protected boolean doSelect(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {

			return	Select(store,parent,record,property,filter);
			}

			@Override
			protected void onFilter() {
				super.onFilter();

				// grid.getSelectionModel().select(0);
			}

		};
		ajaxfield.setAutoWidth(true);
		ajaxfield.setToolTip("任务清单快速查找");
		ajaxfield.bind(getStore());
		this.ajaxfield=ajaxfield;
		return ajaxfield;
	}
	/************************************/

	
}
